﻿using ConfExpert.Forms.Base;
using DevExpress.Utils;
using DevExpress.XtraEditors.Repository;
using DevTeam;

using System;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.Araclar
{
    public partial class YeniMusteri : XForm
    {
        public YeniMusteri()
        {
            InitializeComponent();
        }

        private void YeniMusteri_Load(object sender, EventArgs e)
        {
            gridView1.Init("GetYeniMusteri", string.Empty);
            gridView1.FormatAsPercentage("Score");
            gridView1.MakeColumnInvisible("CariId", "CustomerId");
            gridView1.MakeReadOnly();

            //((RepositoryItemTextEdit)txtMusteri.Edit).KeyPress += YeniMusteri_KeyPress;
            this.Shown += YeniMusteri_Shown;

            gridView1.DoubleClick += gridView1_DoubleClick;
            gridView1.RowStyle += gridView1_RowStyle;
            gridView1.BestFitColumns();
            gridView1.MakeRowSelectable();

            btnTanimla.ItemClick += btnTanimla_ItemClick;

            //barManager.EditorKeyPress += barManager_EditorKeyPress;


            ((RepositoryItemTextEdit)txtMusteri.Edit).EditValueChanging += new DevExpress.XtraEditors.Controls.ChangingEventHandler(repositoryItemTextEdit1_EditValueChanging);
            //((RepositoryItemTextEdit)txtMusteri.Edit).KeyDown += new KeyEventHandler(repositoryItemTextEdit1_KeyDown);
        }

        //void repositoryItemTextEdit1_KeyDown(object sender, KeyEventArgs e)
        //{
        //    Debug.WriteLine(e.KeyCode);
        //}

        void repositoryItemTextEdit1_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            if (e.NewValue != null)
            {
                string str = e.NewValue.ToStr();
                gridView1.RefreshDataSource("GetYeniMusteri", str);
                gridView1.BestFitColumns();
            }
        }

        void btnTanimla_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            str = txtMusteri.EditValue.ToStr();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if(e.RowHandle>-1)
                if(gridView1.GetRowCellValue(e.RowHandle, "CustomerId").ToInt()>0)
                    e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
        }

        public int CariId = 0;
        public int CustomerId = 0;
        public string str = string.Empty;
        void gridView1_DoubleClick(object sender, EventArgs e)
        {
            DataRow r = gridView1.GetFocusedDataRow();
            if(r!=null)
            {
                CariId = r["CariId"].ToInt();
                CustomerId = r["CustomerId"].ToInt();
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        void YeniMusteri_Shown(object sender, EventArgs e)
        {
            txtMusteri.Links[0].Focus();
            //(barEditItem1.Links[0] as DevExpress.XtraBars.BarEditItemLink).ShowEditor();
        }

    }
}
